﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Threading;

using SSATool.DL.Common.Dto;
using SSATool.Common.Util;

#endregion

namespace SSATool.DL.ServiceModel
{
    /// <summary>
    /// Contains the facilities to retrieve and store process service
    /// information and status.
    /// </summary>
    internal class ServiceProcessWatcher : DisposeBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private const int StartTimeout = 5000; //ms
        private const int Interval = Timeout.Infinite;  //disable periodic calls

        private readonly Action _doWatchWork;
        private Timer _timer;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceProcessWatcher"/> class.
        /// </summary>
        internal ServiceProcessWatcher(Action doWatchWork)
        {
            PreCondition.AssertNotNull(doWatchWork, "doWatchWork");

            _doWatchWork = doWatchWork;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Starts the watch interval.
        /// </summary>
        internal void Start()
        {
            lock (this)
            {
                EnsureNotDisposed();

                _timer = new Timer(OnTimerCallback, null, StartTimeout, Interval);
            }
        }

        /// <summary>
        /// Stops the watch interval.
        /// </summary>
        internal void Stop()
        {
            lock (this)
            {
                if (_timer != null)
                {
                    _timer.Dispose();
                    _timer = null;
                }
            }
        }

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            Stop();
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        private void OnTimerCallback(object state)
        {
            _doWatchWork();
            
            lock (this)
            {
                if (_timer != null)
                    _timer.Change(StartTimeout, Interval);
            }
        }

        #endregion
    }
}
